package comm.parsers;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import comm.util.SoptHashMap;

public class NaverLocalResultSAXParserHandler extends DefaultHandler {
	  private StringBuffer title = new StringBuffer();
	  private StringBuffer link = new StringBuffer();
	  private StringBuffer description = new StringBuffer();
	  private StringBuffer telephone = new StringBuffer();
	  private StringBuffer address = new StringBuffer();
	  private StringBuffer mapx = new StringBuffer();
	  private StringBuffer mapy = new StringBuffer();
	  
	  private boolean isTitle = false;
	  private boolean isLink = false;
	  private boolean isDescription = false;
	  private boolean isTelephone = false;
	  private boolean isAddress = false;
	  private boolean isMapx = false;
	  private boolean isMapy = false;
	  
	  
	  private boolean isTotalcount = false;
	  
	  private String currentBuffer = "";
	  private String totalcount = "";
	  
	  public int itemNum=1;
	  
	  public ArrayList itemList = new ArrayList();
	  
	  public ArrayList getItemList(){
		  return itemList;
	  }
	  
	
	  
	  public void startElement(String uri, String localName,
	                     String qName, Attributes atts)
	  {
	      if (qName.equals("title")) {
	    	  currentBuffer = "title";
	    	  isTitle=true;  title.setLength(0); 
	      }else if (qName.equals("link")) {
	    	  isLink=true;  link.setLength(0); 
	      }else if (qName.equals("description")) {
	    	  currentBuffer = "description";
	    	  isDescription=true;  description.setLength(0); 
	      }else if (qName.equals("telephone")) {
	    	  isTelephone=true;  telephone.setLength(0); 
	      }else if (qName.equals("address")) {
	    	  currentBuffer = "address";
	    	  isAddress=true;  address.setLength(0); 
	      }else if (qName.equals("mapx")) {
	    	  isMapx=true;  mapx.setLength(0); 
	      }else if (qName.equals("mapy")) {
	    	  isMapy=true;  mapy.setLength(0); 
	      }else if( qName.equals("total")){
	    	  isTotalcount=true;
	      }
	  }	  
	  
	  public void endElement(String uri, String localName,
	                     String qName)
	 { 
		  currentBuffer = "";
		  
		  if (qName.equals("item")){
	          //저장!~
			  SoptHashMap tempHm = new SoptHashMap();
			  
			  tempHm.put("totalcount",totalcount);
			  tempHm.put("title", title.toString());
			  tempHm.put("link", link.toString());
			  tempHm.put("description", description.toString());
			  tempHm.put("telephone", telephone.toString());
			  tempHm.put("address", address.toString());
			  tempHm.put("mapx", mapx.toString());
			  tempHm.put("mapy", mapy.toString());
			  
			  itemList.add(tempHm);
			  
	          itemNum++;
	      }else if (isTitle) {
	    	  isTitle=false; 
	      }else if (isLink) {
	    	  isLink=false;
	      }else if (isDescription) {
	    	  isDescription=false;
	      }else if (isTelephone) {
	    	  isTelephone=false;
	      }else if (isAddress) {
	    	  isAddress=false;
	      }else if (isMapx) {
	    	  isMapx=false;
	      }else if (isMapy) {
	    	  isMapy=false;
	      }else if(isTotalcount){
	    	  isTotalcount=false;
	      }
	  }
	  public void characters(char[] chars, int start, int leng) 
	  { 
		  String temp = new String(chars, start, leng);
		 
	      if (isTitle) {
	    	  isTitle=false; title.append(chars,start,leng);
	      }else if (isLink) {
	    	  isLink=false;  link.append(chars,start,leng);
	      }else if (isDescription) {
	    	  isDescription=false;  description.append(chars,start,leng);
	      }else if (isTelephone) {
	    	  isTelephone=false;  telephone.append(chars,start,leng);
	      }else if (isAddress) {
	    	  isAddress=false;  address.append(chars,start,leng);
	      }else if (isMapx) {
	    	  isMapx=false;  mapx.append(chars,start,leng);
	      }else if (isMapy) {
	    	  isMapy=false;  mapy.append(chars,start,leng);
	      }else if(isTotalcount){
	    	  isTotalcount=false; totalcount = temp;
	      }else{ //<b>태그 문제 해결 부분- currentBuffer를 통해 < 태그 이후에 글씨들을 받아서 append
	    	  
	    	  if(currentBuffer.equals("title"))
	    		 title.append(temp);
	    	  else if(currentBuffer.equals("description"))
	    		  description.append(temp);
	    	  else if(currentBuffer.equals("address"))
	    		  address.append(temp);
	      }
	  }
	
}
